package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortCartData;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_SET_EVENT_AUTO_HANDLE, desc = "设置事件是否自动处理")
public class CrossBeautyEscortSetEventAutoHandleCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortSetEventAutoHandleReqMsg reqMsg = BeautyEscortProto.BeautyEscortSetEventAutoHandleReqMsg.parseFrom(packet.getBytes());
        CrossEscortPlayerWrap playerWrap = CrossBeautyEscortMgr.getEscortPlayer(serverId, packet.getPlayerId());
        if(playerWrap != null){
            for (Map.Entry<Integer, EscortCartData> dataEntry : playerWrap.getCartDataMap().entrySet()) {
                synchronized (dataEntry.getValue()){
                    dataEntry.getValue().setAutoHandleEvent(reqMsg.getAuto());
                    playerWrap.autoHandleCartEvent(dataEntry.getValue());//触发事件处理
                }
            }
            BeautyEscortProto.BeautyEscortSetEventAutoHandleRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortSetEventAutoHandleRespMsg.newBuilder();
            respMsg.setRet(0);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_SET_EVENT_AUTO_HANDLE, respMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
